<?php
    require_once "config.php";
    //本地调试
    // $add_sql = $mysql->query("select * from dyspw_china where Name LIKE  '%齐齐哈尔%'");
    // $rows = $add_sql->fetch_all(MYSQLI_ASSOC);
    // $_SESSION['Address'] = $rows[0];
    // print_r($_SESSION['Address']);exit;

    if($_SESSION['Address']['Name'] == ''){
    $add_info = GitCityApi();
    $add_info = std_class_object_to_array($add_info);
    $add_sql = $mysql->query("select * from dyspw_china where Name = '".$add_info['city']."'");
    $rows = $add_sql->fetch_all(MYSQLI_ASSOC);
    $_SESSION['Address'] = $rows[0];

    // print_r($_SESSION['Address']);
    }

    // 获取当前用户ip
    function get_client_ip() {
    $ip = $_SERVER['REMOTE_ADDR'];
    if (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
        foreach ($matches[0] AS $xip) {
            if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
                $ip = $xip;
                break;
            }
        }
    }
    return $ip;
    }

    //根据ip获取城市信息
    function GitCityApi(){
    $ip = get_client_ip();
    $url = "http://restapi.amap.com/v3/ip?key=4fa30b7cff4640474633c27b835646fd&ip=".$ip;
    $res = file_get_contents($url);
    return json_decode($res);
    }

    function std_class_object_to_array($stdclassobject)
    {
    $_array = is_object($stdclassobject) ? get_object_vars($stdclassobject) : $stdclassobject;
    foreach ($_array as $key => $value) {
        $value = (is_array($value) || is_object($value)) ? std_class_object_to_array($value) : $value;
        $array[$key] = $value;
    }
    return $array;
    }

    //判定时间区域
    function checkIsBetweenTime($start,$end){
    $date= date('H:i');
    $curTime = strtotime($date);//当前时分
    $assignTime1 = strtotime($start);//获得指定分钟时间戳，00:00
    $assignTime2 = strtotime($end);//获得指定分钟时间戳，01:00
    $result = true;
    if($curTime>$assignTime1&&$curTime<$assignTime2){
        $result = false;
    }
    return $result;
    }

 ?>
